package org.flik.server.bean;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.flik.server.entity.FlikUser;

@Stateless
@LocalBean
public class FlikUserBean {
	
    @PersistenceContext(unitName = "flik", type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;
        
	public FlikUser createUser(String userName, String firstName, String lastName, String password, String email) {
		FlikUser flikUser = new FlikUser(userName, password, firstName, lastName, email);
		entityManager.persist(flikUser);
		return flikUser;
	}
	
	public FlikUser fetchUser(String userName, String password){
		@SuppressWarnings("unchecked")
		List<FlikUser> result = entityManager.createQuery("select fu from FlikUser fu where fu.userName=:userName and fu.password=:password").setParameter("userName", userName).setParameter("password", password).getResultList(); 
		return result.size() == 0? null: result.get(0);
	}
	public boolean isUserNameExist(String userName){
		return entityManager.createQuery("select fu from FlikUser fu where fu.userName=:userName").setParameter("userName", userName).getResultList().size() != 0;
	}
}
